import Dep from './dep.js'

function Observer (data) {
  this.oberver(data)
}

Observer.prototype.oberver = function (data) {
  if (data && typeof data === 'object') {
    Object.keys(data).forEach(key => {
      this.defineReactive(data,key, data[key])
    })
  }
}

Observer.prototype.defineReactive = function (data, key, value) {
  let dep = new Dep()
  this.oberver(value)
  Object.defineProperty(data,key, {
    get() {
      console.log('获取get值== ', value)
      const target = Dep.target
      target && dep.addWatcher(target)
      return value
    },
    set(newVal) {
      if (value === newVal) return
      console.log('设置set值== ', newVal)
      value = newVal
      dep.notify()
    }
  })
}
export default Observer